/******************************************************************************
 *  Copyright © 2019 Shenzhen ECIOT Technology Co., Ltd All Rights Reserved
 *-----------------------------------------------------------------------------
 * @file        main.c
 * @brief       app demo
 * @author      mo@eciot.cn (qq:2201920828,v:eciot_mo)
 * @date        2022-02-13
 * @version     1.0
 ******************************************************************************/

#include "main.h"

#ifdef EC_APP_MAIN

#include "ec_core.h"
#include "ec_app_flash.h"
#include "ec_app_ble_peripheral.h"
#include "ec_app_ble.h"

#define EC_APP_UART0_TX_BUF_SIZE 1024                 //串口0发送缓冲区大小，可以根据需要调整
#define EC_APP_UART0_RX_BUF_SIZE 1024                 //串口0接收缓冲区大小，可以根据需要调整
#define EC_APP_UART1_TX_BUF_SIZE 1024                 //串口1发送缓冲区大小，可以根据需要调整
#define EC_APP_UART1_RX_BUF_SIZE 1024                 //串口1接收缓冲区大小，可以根据需要调整
uint8_t uart0_tx_buf[EC_APP_UART0_TX_BUF_SIZE] = {0}; //串口0发送缓冲区
uint8_t uart0_rx_buf[EC_APP_UART0_RX_BUF_SIZE] = {0}; //串口0接收缓冲区
uint8_t uart1_tx_buf[EC_APP_UART1_TX_BUF_SIZE] = {0}; //串口1发送缓冲区
uint8_t uart1_rx_buf[EC_APP_UART1_RX_BUF_SIZE] = {0}; //串口1接收缓冲区

void uart0_rx(uint8_t *buf, uint16_t len)
{
	
	    if ((buf[0] == 'A') && (buf[1] == 'T'))
    {
        // The AT command is not supported.
        ec_core_uart0_printf("The AT command is not supported.\r\n");
    }
		else
    ec_core_ble_send(buf, len); //串口数据转发到蓝牙

    
}

void uart1_rx(uint8_t *buf, uint16_t len) //串口1接收数据中断
{
    if ((buf[0] == 'A') && (buf[1] == 'T'))
    {
        // The AT command is not supported.
        ec_core_uart1_printf("The AT command is not supported.\r\n");
    }
		else
    ec_core_ble_send(buf, len); //串口数据转发到蓝牙
}

void uart_init(void) //串口1初始化 波特率精度受时钟频率影响
{   
	ec_core_uart_init(EC_CORE_UART0, 115200, EC_CORE_UART_PARITY_NONE,
                      EC_CORE_GPIO_P4, EC_CORE_GPIO_P5,
                      uart0_tx_buf, 1024, uart0_rx_buf, 1024,
                      uart0_rx);
	
	
  ec_core_uart_init(EC_CORE_UART1, 115200, EC_CORE_UART_PARITY_NONE,
                      EC_CORE_GPIO_P2, EC_CORE_GPIO_P1,
                      uart1_tx_buf, EC_APP_UART1_TX_BUF_SIZE, uart1_rx_buf, EC_APP_UART1_RX_BUF_SIZE,
                      uart1_rx);
}

int main(void)
{
    ec_core_sys_clk_set(EC_CORE_SYS_CLK_48M); //配置系统时钟

    ec_app_flash_sys_param_read(); // 从flash读取系统参数
    ec_app_ble_param_init();       // 初始化蓝牙相关的参数

    ec_core_init(); //蓝牙内核初始化

    //串口0初始化 波特率精度受时钟频率影响

    uart_init();

    uint8_t ver[3] = {0};
    ec_core_ver(ver);                                                       //读取软件版本
    ec_core_uart0_printf("ECB02 SDK %d.%d.%d\r\n", ver[0], ver[1], ver[2]); //串口0 printf打印
    ec_core_uart0_printf("ECB02 SDK %d.%d.%d\r\n", ver[0], ver[1], ver[2]); //串口0 printf打印
    ec_core_sw_watchdog_init(EC_CORE_SW_TIMER6, 2, 3); //初始化软件看门狗，广播超时时间2分钟，蓝牙连接超时时间3分钟

		
//						 uint8_t data1[] = {0x17,0x09,0x48,0x59,0x50,0x5F,0x43,0x35,0x44,0x34,0x37,0x46,0x34,0x34,0x32,0x36,0x30,0x38};
////				 uint8_t data2[] = {0x10,0x77,0x7E,0x40,0x5D,0x2A,0xF9,0xFA,0x98,0xFE,0x54,0xA7,0xDF,0xE4,0x35,0x35};

//		ec_core_ble_peripheral_set_custom_adv_data(data1,sizeof(data1));
		
//		 uint8_t temp_scan_rsp[] = {0x17, 0x09,0x48,0x59,0x50,0x5F,0x43,0x35,0x44,0x34,0x37,0x46,0x34,0x34,0x32,0x36,0x30,0x38};
//        ec_core_ble_peripheral_set_custom_scan_rsp(temp_scan_rsp, sizeof(temp_scan_rsp));
    ec_core_sleep_disable(); //禁止睡眠，串口可以接收数据
    ec_core_main_loop_run(); //系统主循环
}

#endif

///******************************************************************************
// *  Copyright © 2019 Shenzhen ECIOT Technology Co., Ltd All Rights Reserved
// *-----------------------------------------------------------------------------
// * @file        ec_app_custom_adv_test.c
// * @brief       custom advert demo
// * @author      mo@eciot.cn (qq:2201920828,v:eciot_mo)
// * @date        2022-02-13
// * @version     1.0
// ******************************************************************************/

//#include "main.h"



//#include "ec_core.h"

//#define EC_APP_UART0_TX_BUF_SIZE 1024                 //串口0发送缓冲区大小，可以根据需要调整
//#define EC_APP_UART0_RX_BUF_SIZE 1024                 //串口0接收缓冲区大小，可以根据需要调整
//uint8_t uart0_tx_buf[EC_APP_UART0_TX_BUF_SIZE] = {0}; //串口0发送缓冲区
//uint8_t uart0_rx_buf[EC_APP_UART0_RX_BUF_SIZE] = {0}; //串口0接收缓冲区

////自定义广播包
//uint8_t adv_data[] = {
//    0x02, 0x01, 0x06,      // ble flag
//		0x11, 0x09, 0x48,0x59,0x50,0x5F,0x43,0x35,0x44,0x34,0x37,0x46,0x34,0x34,0x32,0x36,0x30,0x38
////	0x05, 0x09, 0xf0, 0xff,0xf0, 0xff  // server
//};

////自定义扫描应答包
//uint8_t scan_rsp[] = {
//0x11, 0x07,0x77, 0xE4, 0x05, 0xD2, 0xAF, 0x9F, 0xA9, 0x8F, 0xE5, 0x4A, 0x7D, 0xFE, 0x43, 0x53, 0x53, 0x49
////	    0x06,
////    0x09,
////    'E',
////    'C',
////    'I',
////    'O',
////    'T',
//	
//};

//void uart0_rx(uint8_t *buf, uint16_t len) //串口0接收数据中断
//{
//    if (strcmp((const char *)buf, "AA") == 0) //修改广播包内容，重新广播
//    {
//        adv_data[5] = 0XE0;
//        ec_core_ble_peripheral_set_custom_adv_data(adv_data, sizeof(adv_data));
//        ec_core_ble_peripheral_reset_adv();
//        ec_core_uart0_printf("set custom adv data\r\n");
//    }
//    if (strcmp((const char *)buf, "BB") == 0) //关闭自定义广播包，使用系统默认的
//    {
//        ec_core_ble_peripheral_clear_custom_adv_data();
//        ec_core_ble_peripheral_reset_adv();
//        ec_core_uart0_printf("clear custom adv data\r\n");
//    }
//    if (strcmp((const char *)buf, "CC") == 0) //设置自定义扫描应答包
//    {
//        uint8_t temp_scan_rsp[] = {0x09, 0x09, 'E', 'C', 'I', 'O', 'T', '_', 'B', 'T'};
//        ec_core_ble_peripheral_set_custom_scan_rsp(temp_scan_rsp, sizeof(temp_scan_rsp));
//        ec_core_ble_peripheral_reset_adv();
//        ec_core_uart0_printf("set custom scan rsp\r\n");
//    }
//    if (strcmp((const char *)buf, "DD") == 0) //关闭自定义的扫描应答包，使用系统默认的
//    {
//        ec_core_ble_peripheral_clear_custom_scan_rsp();
//        ec_core_ble_peripheral_reset_adv();
//        ec_core_uart0_printf("clear custom scan rsp\r\n");
//    }
//}

//void uart0_init(void) //串口0初始化 波特率精度受时钟频率影响
//{
//    ec_core_uart_init(EC_CORE_UART0, 115200, EC_CORE_UART_PARITY_NONE,
//                      EC_CORE_GPIO_P4, EC_CORE_GPIO_P5,
//                      uart0_tx_buf, EC_APP_UART0_TX_BUF_SIZE, uart0_rx_buf, EC_APP_UART0_RX_BUF_SIZE,
//                      uart0_rx);
//}

//int main(void)
//{
//    ec_core_sys_clk_set(EC_CORE_SYS_CLK_48M); //系统时钟初始化

//    ec_core_ble_peripheral_set_custom_adv_data(adv_data, sizeof(adv_data)); //初始化默认自定义广播包
//    ec_core_ble_peripheral_set_custom_scan_rsp(scan_rsp, sizeof(scan_rsp)); //初始化默认自定义扫描应答包

//    ec_core_init(); //蓝牙内核初始化

//    uart0_init();                                       //串口0初始化 波特率精度受时钟频率影响
//    ec_core_uart0_printf("ec_app_custom_adv_test\r\n"); //串口0 printf打印

//    ec_core_sleep_disable(); //禁止睡眠，串口可以接收数据
//    ec_core_main_loop_run(); //系统主循环
//}



